﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows;

namespace FlagEnum
{
    [Flags]
    public enum DayOfWeek
    {
        Monday = 0x1,
        Tuesday  = 0x2,
        Wednesday = 0x4,
        Thursday = 0x8,
        Firdy = 0x10,
        Saturday = 0x20,
        Sunday = 0x40
    }

    public class TestFlagEnum
    {
        public DayOfWeek MultiValue { get; set; }
        public DayOfWeek SingleValue { get; set; }

        public static TestFlagEnum Create()
        {
            
            TestFlagEnum ret = new TestFlagEnum();
            ret.MultiValue = DayOfWeek.Monday | DayOfWeek.Saturday | DayOfWeek.Thursday;
            ret.SingleValue = DayOfWeek.Wednesday;
            return ret;
        }
    }



    public class FlagEnumConverter : IMultiValueConverter
    {
        public string targetData {get;set;}

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            
            targetData = values[0].ToString();

            if (targetData.Trim() == "0") return false;

            if(targetData.Contains(values[1].ToString()))
                return true;
            else
                return false;


        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            
            return new object[] { null,null };
        }

        
    }

    public class FlagCheckBox : System.Windows.Controls.CheckBox
    {
        protected override void OnClick()
        {
            if (this.IsChecked.HasValue && this.IsChecked.Value == false)
            {
                
                var tmp = MyContent.ToString();
                if (tmp == "0") 
                    tmp = this.Content.ToString();
                else
                    tmp = tmp + "," + this.Content.ToString();
                this.MyContent = Enum.Parse(this.MyContent.GetType(), tmp);
            }
            else
            {

                var tmp = MyContent.ToString();
                var tmpArray = tmp.Split(',');
                tmp = "";
                foreach (var item in tmpArray)
                {
                    if(item.ToString().Trim() != this.Content.ToString().Trim())
                        tmp += item + ",";
                }

                if (tmp.Length > 0) tmp = tmp.Substring(0, tmp.Length - 1);
                if(tmp.Length > 0 )
                    this.MyContent = Enum.Parse(this.MyContent.GetType(), tmp);
                else
                    this.MyContent = Enum.Parse(this.MyContent.GetType(), "0");
            }

            base.OnClick();
        }
        #region DependencyProperty Content

        /// <summary>
        /// Registers a dependency property as backing store for the Content property
        /// </summary>
        public static readonly DependencyProperty MyContentProperty =
            DependencyProperty.Register("MyContent", typeof(object), typeof(FlagCheckBox),
            new FrameworkPropertyMetadata(null,
                  FrameworkPropertyMetadataOptions.AffectsRender |
                  FrameworkPropertyMetadataOptions.AffectsParentMeasure));

        /// <summary>
        /// Gets or sets the Content.
        /// </summary>
        /// <value>The Content.</value>
        public object MyContent
        {
            get { return (object)GetValue(MyContentProperty); }
            set { SetValue(MyContentProperty, value); }
        }

        #endregion
        
    }
}
